import { getListBrand } from '@/api/brand'
import {getListCarType, getListCarSource} from '@/nodeApi/filter'
export default {
  state: {
      // 页面选择后传过来的参数
      brand: '',
      price: '',
      carType: '',
      carSource: '',       
      // 品牌数据
      brandList: [],
      // 车型数据
      carTypes: [],
      // 产地数据
      sourceCountrys: [],
      isNewEnergy: '',
      // 价格,后台无数据自定义
      priceList: [
          { name: "8万以下", minAllPrice: '0', maxAllPrice: '8' },
          { name: "8-12万", minAllPrice: '8', maxAllPrice: '12' },
          { name: "12-18万", minAllPrice: '12', maxAllPrice: '18' },
          { name: "18-25万", minAllPrice: '18', maxAllPrice: '25' },
          { name: "25-40万", minAllPrice: '25', maxAllPrice: '40' },
          { name: "40万以上", minAllPrice: '25', maxAllPrice: '100000000'}
      ],
      // activeBrand点击过后添加样式
      activeBrand: true,
      activePrice: false,
      activeType: false,
      activeCountry: false,
      activeIsNewEnergy: false,
      // 小方块显示
      actBrand: false,
      actPrice: false,
      actCarType: false,
      actCountry: false,
      actIsNewEnergy: false,
  },
  actions: {
    //获取品牌数据 提交突变
    handlerGetListBrand({commit,state}){
        getListBrand().then(resp => {
            //console.log(resp.data);
            commit("GetListBrand",resp.data.data);
        })
    },
    //获取车型数据 提交突变
    handlerGetCarTypes({commit,state}) {
        getListCarType().then(resp => {
            // //console.log(resp.data);
            commit("GetCarTypes",resp.data.carTypes);
        })
    },
    //获取产地数据 提交突变
    handlerGetCarSource({commit,state}){
        getListCarSource().then(resp => {
            // //console.log(resp.data);
            commit("GetCarSource",resp.data.carSources);
        })
    },
    //获取选择后的数据提交给突变改变数据
    choiceBrand({commit},brand){
         commit("changeBrand",brand);
    },
    choicePrice({commit},price){
         commit("changePrice",price);
    },
    choiceCarType({commit},carType){
         commit("changeCarType",carType);
    },
    choiceIsNewEnergy({ commit }, isNewEnergy){
        commit("changeIsNewEnergy", isNewEnergy);
    },
    choiceSourceCountry({commit},sourceCountry){
         commit("changeSourceCountry", sourceCountry);
    },
    //提交方法改变样式
    changeBrand({commit,state}){
        commit("changeActiveBrand");
    },
    changePrice({commit,state}){
        commit("changeActivePrice");
    },
    changeCarType({commit,state}){
        commit("changeActiveCarType");
    },
    changeCountry({commit,state}){
        commit("changeActiveCountry");
    },
    clearallchioses({commit,state}){
        commit("clearallchioses");
    },
    closCondition({commit},ele){
        commit("closFilter",ele);
    }   
  },
  mutations: {  
    // axios拿到数据付给仓库全局数据
    GetListBrand(state,data){
        state.brandList=data;
    },
    GetCarTypes(state,data){
        state.carTypes=data;
    },
    GetCarSource(state,data){
        state.sourceCountrys=data;
    },
    // 拿到选择后的值付给仓库全局数据
    changeBrand(state,data){
        state.brand=data;
        state.actBrand=true;
    },
    changePrice(state,data){
        state.price=data;
        state.actPrice=true;
    },
    changeCarType(state,data){
        state.carType=data;
        state.actCarType=true;
    },
    changeIsNewEnergy(state, data){
        if(data.status === 1){
            state.isNewEnergy = data;
            state.actIsNewEnergy = true;
            //console.log(1)
        }else{
            state.isNewEnergy = '';
            state.actIsNewEnergy = false;
            //console.log(1222)
        }
    },
    changeSourceCountry(state,data){
        state.carSource=data;
        state.actCountry=true;
    },
    changeActiveBrand(state){
        state.activePrice=false;
        state.activeBrand=true;
        state.activeType=false;
        state.activeCountry=false;
    },
    // 改变状态样式 全局付给仓库
    changeActivePrice(state){
        state.activePrice=true;
        state.activeBrand=false;
        state.activeType=false;
        state.activeCountry=false;
    },
    changeActiveCarType(state){
        state.activePrice=false;
        state.activeBrand=false;
        state.activeType=true;
        state.activeCountry=false;
    },
    changeActiveCountry(state){
        state.activePrice=false;
        state.activeBrand=false;
        state.activeType=false;
        state.activeCountry=true;
    },
    // 页面一加载就重新定义数据
    clearallchioses(state){
         state.actBrand = false;
         state.actPrice = false;
         state.actCarType = false;
         state.actCountry = false;
         state.actIsNewEnergy = false;
    },
    // 关闭条件
    closFilter (state, data) {
        if (state.brand === data){
            state.brand = ''
            state.actBrand=false;
        }
        if (state.price === data){
            state.price = ''
            state.actPrice=false;
        }
        if (state.carType === data){
            state.carType = ''
            state.actCarType=false;
        }
        if (state.isNewEnergy === data) {
            state.isNewEnergy = ''
            state.actIsNewEnergy = false;
        }
        if (state.carSource === data){
            state.carSource = ''
            state.actCountry=false;
        }
    }
  }
}